package com.leps.nq.tx.core;

import com.leps.nq.bu.service.IBuPlcService;
import com.leps.nq.op.service.IOpAlarmService;
import com.leps.nq.tx.mapper.PumpEffectivePointMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

@Component
public class TxScheduler {
    @Autowired
    PumpEffectivePointMapper pumpEffectivePointMapper;
    @Autowired
    IBuPlcService plcService;
    @Autowired
    IOpAlarmService alarmService;
    @Autowired
    ScheduledExecutorService executor;
    @Autowired
    PlcReadWriteService plcReadWriteService;

    @PostConstruct
    public void scheduleRead() {
        int intervalHours = SysConfigs.plcFetchInterval();
        executor.scheduleAtFixedRate(() -> {
            this.plcReadWriteService.fetchAllPlc();
        }, 3, intervalHours * 60, TimeUnit.MINUTES);
    }

    @PostConstruct
    public void scheduleWriteRetry() {
        int intervalMinutes = SysConfigs.plcReconnectInterval();
        executor.scheduleAtFixedRate(() -> {
            this.plcReadWriteService.writeRetry();
        }, 3, intervalMinutes, TimeUnit.MINUTES);
    }
}
